class PopsiclesController < ApplicationController

skip_before_filter :store_path, :only => [:create_public_response]
skip_before_filter :authorise_user, :only => [:new]

  # GET /popsicles
  # GET /popsicles.xml
  def index
    @popsicles = Popsicle.all

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @popsicles }
    end
  end

  # GET /popsicles/1
  # GET /popsicles/1.xml
  def show
  	@entity = Entity.find(params[:id])
  	@private_messages = @entity.get_private_messages.sort! {|p1,p2| p2.created_at <=> p1.created_at}
	@private_messages.each do |pm|
		pm.update_attribute(:updated_at, Time.now)
	end

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @popsicle }
    end
  end
  
  def show_sent #Michael
  	@entity = Entity.find(params[:id])
  	@sent_messages = @entity.get_sent_messages.sort! {|p1,p2| p2.created_at <=> p1.created_at}

    respond_to do |format|
      format.html # show_sent.html.erb
      format.xml  { render :xml => @popsicle }
    end
  end

  # GET /popsicles/new
  # GET /popsicles/new.xml
  def new		# this is the page
  	@entity = Entity.find(params[:id])
  	@permissible_friends = @entity.find_permissible_friends

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @popsicle }
    end
  end

  def new_to_group
    @entity = Entity.find(params[:id])
    @friend_groups = @entity.puddles

    respond_to do |format|
      format.html
      format.xml { render :xml => @popsicle }
    end
  end

  # GET /popsicles/1/edit
  def edit
    @popsicle = Popsicle.find(params[:id])
  end

  # POST /popsicles
  # POST /popsicles.xml
  def create	# this is the action
  
  # this code doesn't appear to be used
  
  	if true
  		@popsicle = PublicPopsicle.new(params[:popsicle])
  		#@popsicle.public_author_id = @entity.id
  	else
  		@popsicle = PublicPopsicle.new(params[:popsicle])
  		#@popsicle.private_author_id = @entity.id
  	end
		
    respond_to do |format|
      if @popsicle.save
        format.html { redirect_to(popsicle_url(@popsicle), :notice => 'Popsicle was successfully created.') }
        format.xml  { render :xml => popsicle_url(@popsicle), :status => :created, :location => @popsicle }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @popsicle.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /popsicles/1
  # PUT /popsicles/1.xml
  def update
    @popsicle = Popsicle.find(params[:id])

    respond_to do |format|
      if @popsicle.update_attributes(params[:popsicle])
        format.html { redirect_to(@popsicle, :notice => 'Popsicle was successfully updated.') }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @popsicle.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /popsicles/1
  # DELETE /popsicles/1.xml
  def destroy
    @popsicle = Popsicle.find(params[:id])
	  @entity = Entity.find(@popsicle.public_author_id)
    @popsicle.destroy

    respond_to do |format|
      format.html { redirect_to(entity_path(@entity)) }
      format.xml  { head :ok }
    end
  end
  
  def show_private_response
	 @popsicle = Popsicle.find(params[:popsicle])
  end
  
  def show_public_response
	 @popsicle = Popsicle.find(params[:popsicle])
   #session[:return_to] = request.referer
  end
  
  def create_public_response #Adrian
    
  	popsicle = Popsicle.find(params[:old_popsicle_id])
  	popsicle.find_end_response.create_response(session[:user_id], params[:content])
  	original_author = Entity.find(popsicle.find_original_popsicle.public_author_id)
  	if original_author.id == session[:user_id]
  		redirect_to(session[:return_to])
  	else
  		redirect_to(session[:return_to])
  	end
  end
  
  def create_private_response #Michael
  	popsicle = Popsicle.find(params[:old_popsicle_id])

	 respond_to do |format|
      if popsicle.create_response(session[:user_id], params[:content])
        format.html { redirect_to(entity_path(session[:user_id]), :alert => 'You successfully sent a private message to ' + Entity.find(popsicle.private_author_id).name) }
        format.xml  { head :ok }
      else
        format.html { render :action => "create_private_response", :alert => 'There was a problem sending your personal message' }
      end
    end
  end
  
end
